package com.ruoyi.web.service.pay;

import com.alipay.api.*;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;

import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlipayCertService {

    public static String APPID = "2021005171659673";
    public static String RSA_PRIVATE_KEY = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCWZmLOKtKyBfxDdVjYxk0bSXis5nNDNUPh0ID2qYEZXu2f89ZBXTtTFXgN0TzWdRz9cc11ceEL878lHl1bkbrMHklWl22uYx0gGht80x5d57WFbVd41R/pycDlDlrEH+ssGwOW0bKp7S1EXID9Bij9MVwIDkSF3uMl0B4H46aBVyST/CQ7/+ULg/clE3DmxdQN7DGIlcHHWShLy4guUOZLmSEAzZ3eIHILXmxW5c+tInKysP3BaiSBvxTHmH/QsATxFje6Laas4JMBiJtWLOp4ih5bqBOc1JVrB+bK4IrzkKcAjUKEvCeyxDPWwcvMoh/JLnqJuCJWGDqRSyWbdMFBAgMBAAECggEAOnUnWF0aselTqOC2nONO2xlOl12nVWshrGa/iaBOto7IK9C56YQUZ/HDTROT4wN4NiaCpnCR+XRPSl0oCkx1X5syBD8teYe6rVyYbIcemX9Tdb4PLr8f/V3W5PEUZIDHhsglN/zfxd6TJqwR0scPLYJ+8/6fsaz1eyAOXCCm+kRNNeQciX0zLXltnMQLV5LNBYN11JJeKJOKda91fKIDQxqgnDiVRIUCKHF54O3aqwkWXAFfLBDoYyAo1/+OMaM/19jAupSE5lJXj4rpvdd0qHsII94Kz5n/nhqtGUclcZYg4vxJRGrVRVvdMOsHYj5GG1CyE+j0oSv5KuyD9SB2VQKBgQDI9MIOXZxoPI8aG8X5Y27C77UJH6wjIIjt8NVbA7nZsHDjeqzm8+sBeLSU3A0f4ZcUHuR3n3rgYko1C4z9f4VyQwxPaW9LHa0gWbeOdSf8EW2bEYlWfPVVrGEN6WSLBwkxc+ptp/RsusZCvuD0HPO4sd7Y6MIm5dD++ihnFaGpswKBgQC/mJSpFUDhEa50CJKJ1niY5gMGmg3sKq46mrLZyCL5db4gc4N1oUYvoE4F4tMu9QCltZRF62xkbs4BprcUCbzNuMaoebG67yJssnb15b2fXH3mV4jon7VJ5ODiERux2WsqAOGKQVkdXWx+pc9pvPNUxjiDQxW21kSoFvP2nVZHOwKBgEpv/4H6wvkJlaiCKS9b2adzy4d5p/XLE22wDBgOpUUxyl85Ddru91owMBQD/wiUM7ZMSyzUcluzjPFhOfScy/2rQGAaGhOlBoA5O3kVhJ4mhocuU8UpvGbj13+juI6XKJyc53V/BVdRSCdpNUK3hsAfgwCuF1z5PBV27IhixuNpAoGBAI1fouIYoqTwjQ9le+tla9s2bakjZ36KB8GKgMod2WDFNqNvWOMlO0rNOIUzV6zgb7fWgjANhQ+eizfAsnqArOYHn96U3UYYSgh2rzzGZuL+NpAOqyErJ3P9BMsK4KqcyWxW9hU0HvOCChpxctiM6ef106FXkOA2vrZhKWchtZYdAoGBAK/IUt2QaLCVQiegqmVB9NZJIMARPY/CLAnTiEaVkkf+VQu4JMtrGLJ5p36ojrJIrNmxXQ09iBDvXGXppcu4D3cu/8gvYLM+JhPgi5XLbeYGe9KwD/x8r46rc6Ml5T+yLQuBfBcYxh9XLANj7sopS3bKtR39ELr5oerc7ma+0VnQ";

    public static String creatOrder(Map<String, String> params) {
        String sign;
        //构造支付宝请求对象
        try {
            AlipayClient alipayClient = AlipayUtil.buildAliClient(APPID, RSA_PRIVATE_KEY);
            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
            //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.setBody(params.get("body"));//对一笔交易的具体描述信息。如果是多种商品，请将商品描述字符串累加传给body。例：Iphone6 16G
            model.setSubject(params.get("subject")); //商品的标题/交易标题/订单标题/订单关键字等。
            model.setOutTradeNo(params.get("tradeNo"));//商户网站唯一订单号
            model.setTimeoutExpress("45m");//该笔订单允许的最晚付款时间，逾期将关闭交易。取值范围：1m～15d。m-分钟，h-小时，d-天，1c-当天（1c-当天的情况下，无论交易何时创建，都在0点关闭）。 该参数数值不接受小数点， 如 1.5h，可转换为 90m。
            model.setProductCode("QUICK_MSECURITY_PAY");
            model.setTotalAmount(params.get("amount")); //订单总金额，单位为元，精确到小数点后两位，取值范围[0.01,100000000]
            model.setPassbackParams(null);//设置公共参数，非必填
            request.setBizModel(model);
            request.setNotifyUrl(params.get("notifyUrl"));
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            sign = response.getBody();
        } catch (AlipayApiException e) {
            log.info("支付宝生成订单出错，请检查代码参数,错误信息:{}", e.getMessage());
            return null;
        }
        return sign;
    }
}

